package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.CommunityDao;
import com.hs.panghu.dao.ShopDao;
import com.hs.panghu.dao.impl.CommunityDaoImpl;
import com.hs.panghu.dao.impl.ShopDaoImpl;
import com.hs.panghu.entity.Community;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        CommunityDao communityDao = new CommunityDaoImpl();
        if ("communityList".equals(action)) {
            Map<String, String> map = new HashMap<>();
            String communityName = req.getParameter("communityName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");

            map.put("communityName", communityName);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            //连接数据库

            List<Community> list = communityDao.selectCommunitys(map);


            int count = communityDao.selectCommunityCount(map);

            Map map1 = new HashMap();
            map1.put("list", list);
            map1.put("total", count);


            Result success = Result.success(map1);

            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("communityUpdate".equals(action)) {//增加或修改
            String flag = req.getParameter("addOrUpdateFlag");
            String communityAddress = req.getParameter("communityAddress");
            String communityAreaCode = req.getParameter("communityAreaCode");
            if (communityAreaCode.equals("")){
                communityAreaCode="0";
            }
            String communityCity = req.getParameter("communityCity");
            String communityCoordinates = req.getParameter("communityCoordinates");
            String communityDistrict = req.getParameter("communityDistrict");
            String communityId = req.getParameter("communityId");
            if (communityId.equals("")){
                communityId="0";
            }
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
            //组成对象
            Community community=new Community(
                    Integer.parseInt(communityId),
                    communityName,
                    communityProvince,
                    communityCity,
                    communityDistrict,
                    communityAddress,
                    communityLatitude,
                    communityLongitude,
                    Integer.parseInt(communityAreaCode),
                    communityCoordinates
            );
            if ("1".equals(flag)){//增加
                int row = communityDao.insertCommunity(community);
                if (row!=0){
                    Result success = Result.success();
                    resp.getWriter().write(JSON.toJSONString(success));
                }else {
                    Result error = Result.error();
                    resp.getWriter().write(JSON.toJSONString(error));
                }
            }else if ("2".equals(flag)){
                int row = communityDao.updateCommunity(community);
                if (row!=0){
                    Result success = Result.success();
                    resp.getWriter().write(JSON.toJSONString(success));
                }else {
                    Result error = Result.error();
                    resp.getWriter().write(JSON.toJSONString(error));
                }
            }
        }else if ("communityRemove".equals(action)){
            Integer communityId = Integer.parseInt(req.getParameter("communityId"));
            int row = communityDao.deleteCommunity(communityId);
            if (row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }

        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
